package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.Company;
import com.lsyw.intergrate.entity.TransportPlan;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输公司 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-28
 */
@Api(tags = "运输公司")
@RestController
@RequestMapping("/company")
public class CompanyController {


    @Autowired
    private CompanyService companyService;

    /**
     * 通过类型查询
     */
    @GetMapping("/get-by-type")
    @ApiOperation(value = "通过类型查询", notes = "通过类型查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "type", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "type", required = true, paramType = "query", dataType = "String")
    })
    public Result getByType(Integer type,String name) {
        final LambdaQueryWrapper<Company> lambda = new QueryWrapper<Company>().lambda();
        lambda.eq(Company::getDelFlag,0);
        lambda.eq(Company::getType,type);
        if (name != null && !StringUtils.isEmpty(name)) {
            lambda.like(Company::getName, name);
        }
        return Result.successResult(companyService.list(lambda));
    }


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        return Result.successResult(companyService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody Company company) {
        companyService.save(company);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        Company company = companyService.getById(Long.parseLong(id));
        company.setDelFlag(1);
        this.companyService.updateById(company);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody Company company) {
        companyService.updateById(company);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody CompanyReqVo company) {
        final LambdaQueryWrapper<Company> lambda = new QueryWrapper<Company>().lambda();
        this.buildCondition(lambda, company);
        return Result.successResult(companyService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<Company> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<Company> lambda = new QueryWrapper<Company>().lambda();
        lambda.eq(Company::getDelFlag,0);
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(Company::getName, condition.get("name"));
        }

        return Result.successResult(this.companyService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Company> lambda, CompanyReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(Company::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(Company::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(Company::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(Company::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(Company::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(Company::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(Company::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(Company::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(Company::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getType())) {
            lambda.eq(Company::getType, param.getType());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(Company::getMassifId, param.getMassifId());
        }
        lambda.orderBy(true, false, Company::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class CompanyReqVo extends Company {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
